package com.simp.controller;

import com.simp.aop.SysLog;
import com.simp.dto.SysConfigDto;
import com.simp.entity.SysConfigEntity;
import com.simp.model.SysConfigModel;
import com.simp.page.PageVO;
import com.simp.page.R;
import com.simp.page.SuperController;
import com.simp.service.SysConfigService;
import com.simp.utils.BeanConvertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author pan.shun
 * @since 2022/3/16 15:48
 */
@Api(tags = "参数配置")
@RestController
@RequestMapping("/config")
public class SysConfigController extends SuperController {
    @Autowired
    private SysConfigService sysConfigService;

    @ApiOperation(value = "参数配置分页查询", httpMethod = "GET", notes = "参数配置分页查询，条件为 > configKey=&configValue= ,其中configKey为键，configValue为值 ")
    @GetMapping("/list")
    public R<PageVO<SysConfigEntity>> getPageList(SysConfigDto sysConfigDto) {
        PageVO<SysConfigEntity> pageList = sysConfigService.getPageList(sysConfigDto);
        return success(pageList);
    }

    @SysLog(value = "系统参数配置", addOrUpdate = true)
    @ApiOperation(value = "参数配置新增或修改", notes = "参数配置新增或修改", httpMethod = "POST")
    @PostMapping("/addOrUpdate")
    public R addOrUpdate(@RequestBody SysConfigModel sysConfigModel) {
        SysConfigEntity convert = BeanConvertUtils.convert(sysConfigModel, SysConfigEntity.class);
        return sysConfigService.addOrUpdateConfig(convert);
    }

    @SysLog(value = "系统参数配置删除")
    @ApiOperation(value = "通过ID删除参数配置", notes = "通过ID删除参数配置，多个id以逗号进行隔开", httpMethod = "POST")
    @GetMapping("/removeByIds/{ids}")
    public R removeByIds(@PathVariable String ids) {
        return sysConfigService.removeById(ids);
    }

    @ApiOperation(value = "通过KEY获取参数配置", notes = "通过KEY获取参数配置", httpMethod = "GET")
    @GetMapping("/getConfigKey/{key}")
    public R getConfigKey(@PathVariable String key) {
        return sysConfigService.getConfigByKey(key);
    }
}
